package com.iplay.service.order.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.iplay.component.mailer.Mailer;
import com.iplay.configuration.mail.MailConfigurationProperties;
import com.iplay.service.order.RoleInOrder;
import com.iplay.service.user.UserService;

@Service
public class OrderMailServiceImpl implements OrderMailService {

	@Autowired
	private MailConfigurationProperties mailConfigurationProperties;

	@Autowired
	private Mailer mailer;

	@Autowired
	private UserService userService;

	@Override
	@Async
	public void sendOrderCreatedMail(String receiver, RoleInOrder role, HotelReservation reservation) {
		userService.findByUsername(receiver).ifPresent(user -> {
			String email = user.getEmail();
			mailer.sendMail(mailConfigurationProperties.getSender(), email,
					mailConfigurationProperties.orderCreatedEmail.getSubject(),
					String.format(mailConfigurationProperties.orderCreatedEmail.getContent(), formatRoleInOrder(role),
							reservation.getCustomer(), reservation.getHotel(), reservation.getBanquetHall(), reservation.getNumOfTables(),
							reservation.getDate(), reservation.getRecommender(), reservation.getContact(),
							reservation.getPhone()));
		});
	}

	private String formatRoleInOrder(RoleInOrder role) {
		if (role == null)
			return "管理员";
		switch (role) {
		case CUSTOMER:
			return "客户";
		case RECOMMENDER:
			return "推介人";
		case MANAGER:
			return "经理人";
		}
		return "";
	}

}
